//
//  MyViewController.m
//  tryshopping.boss
//
//  Created by Mr.Lee on 16/11/8.
//  Copyright © 2016年 Reabam. All rights reserved.
//

#import "MyViewController.h"
#import "MyHeaderView.h"
#import "MeDetailCell.h"
#import "MyHeaderViewModel.h"
#import "ACETelPrompt.h"
#import "PersonalInformationViewController.h"   // 个人信息
#import "MyEnshrineViewController.h"            // 我的收藏
#import "SettingViewController.h"               // 设置
#import "AboutUsViewController.h"               // 关于我们
#import "LoginCompanysViewController.h"  // 所属门店

@interface MyViewController () <UITableViewDelegate, UITableViewDataSource, MyHeaderViewDelegate>

/** tabelView */
@property (nonatomic, weak) UITableView *table;
/** 头部 */
@property (nonatomic, weak) MyHeaderView *headerView;
@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置tableView
    [self tableHeadView];
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (@available(iOS 11.0, *)) {
        self.table.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }


    // 请求数据
    [self requestData1];
    // 更新用户信息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(upDataUserInfo) name:@"upDataUserInfo" object:nil];
    // 更新用户信息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(upDataUserInfo) name:@"changeUserInfo" object:nil];
    // 更新用户信息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(upDataUserInfo) name:@"changeTokenId" object:nil];
    
}

// 更新用户信息
- (void)upDataUserInfo{
    [self requestData1];
}


-(void)requestData1{
    [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self.view];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    self.title =@"";
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
    [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self.view];
    [HttpRequest requestData1:@"Common/UserInfo" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        NSLog(@"GetUserAuthStatus === %@",severDataDict.dataDict);
        if (isSuccess) {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                // 设置数据
                self.headerView.dict = severDataDict.dataDict;
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                [user setObject:[[severDataDict.dataDict objectForKey:@"DataLine"] objectForKey:@"fid" ] forKey:@"fid"];
                [user setObject:[severDataDict.dataDict objectForKey:@"headImageUrl"] forKey:@"headImageUrl"];
                [user setObject:[severDataDict.dataDict objectForKey:@"headImage"] forKey:@"headImage"];
                [user setObject:[severDataDict.dataDict objectForKey:@"sex"] forKey:@"sex"];
                
                // 用户名
                [user setObject:[severDataDict.dataDict objectForKey:@"userName"] forKey:@"userName"];
//                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"serverAddress"]] forKey:@"serverAddress"];
//                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"userStatus"]] forKey:@"userStatus"];
//                [user setObject:[NSString stringWithFormat:@"%@", [severDataDict.dataDict objectForKey:@"userCode"]]  forKey:@"userCode"];
//                [user setObject:[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"groupId" ]] forKey:@"groupId"];
                [user synchronize];
                
            } else {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    LoginViewController*detailViewController = [[LoginViewController alloc] init];
                    [self.navigationController pushViewController:detailViewController animated:YES];
                }
                NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:subStr view:self.view];
            }
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            NSString *subStr = [severDataDict.dataDict objectForKey:@"ResultString"];
            [MBProgressHUD showTextHudSub:subStr view:self.view];
        }
    }];
}




- (void)tableHeadView {
    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight -49) style:UITableViewStyleGrouped];
    table.delegate = self;
    table.dataSource = self;
    table.sectionHeaderHeight = 0;
    table.sectionFooterHeight = 15;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    table.backgroundColor=[UIColor whiteColor];
    self.table = table;
    [self.view addSubview:table];

    MyHeaderView *headerView = [MyHeaderView myHeaderView];
    headerView.delegate = self;
    headerView.frame = CGRectMake(0, 0, ScreenWidth, 150);
    self.headerView = headerView;
    table.tableHeaderView = headerView;
}

#pragma mark -- 修改个人信息
- (void)personalDetailsBtnClick:(UIButton *)btn{
    PersonalInformationViewController *pvc = [[PersonalInformationViewController alloc] init];
    pvc.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:pvc animated:YES];
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
#pragma mark--TableViewdatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 4;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier2 = @"Cell2";
    MeDetailCell *cell2=( MeDetailCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier2];
    if (cell2==nil) {
        cell2=[[[NSBundle mainBundle]loadNibNamed:@"MeDetailCell" owner:nil options:nil] lastObject];
    }
    cell2.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 0) {

        cell2.phoneNum.hidden = YES;
        [cell2.img setImage:[UIImage imageNamed:@"suoshupinpai"] forState:UIControlStateNormal];
        cell2.name.text = @"所属品牌";
        
    }else if (indexPath.row == 1){
        
        cell2.phoneNum.hidden = YES;
        [cell2.img setImage:[UIImage imageNamed:@"wodeshoucang"] forState:UIControlStateNormal];
        cell2.name.text = @"我的收藏";
        
    }else if (indexPath.row == 2){

        cell2.phoneNum.hidden = YES;
        [cell2.img setImage:[UIImage imageNamed:@"guanyuwomen"] forState:UIControlStateNormal];
        cell2.name.text = @"关于我们";
    }else if (indexPath.row == 3){
        
        cell2.phoneNum.hidden = YES;
        [cell2.img setImage:[UIImage imageNamed:@"shezhi"] forState:UIControlStateNormal];
        cell2.name.text = @"系统设置";
    }
    
    return cell2;
}

#pragma mark -- tableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) { // 所属品牌
        
        LoginCompanysViewController *CompanyIndustryVC = [[LoginCompanysViewController alloc] init];
        CompanyIndustryVC.view.backgroundColor = [UIColor whiteColor];
        [self.navigationController pushViewController:CompanyIndustryVC animated:YES];
    
    } else if (indexPath.row == 1) { // 我的收藏
        
//        MyEnshrineViewController *enshrineVC = [[MyEnshrineViewController alloc] init];
//        enshrineVC.view.backgroundColor = [UIColor whiteColor];
//        [self.navigationController pushViewController:enshrineVC animated:YES];
        
    } else if (indexPath.row == 2) { // 帮助中心
        
        AboutUsViewController *aboutUSVC = [[AboutUsViewController alloc] init];
        aboutUSVC.view.backgroundColor = zuiqianhuiColor;
        [self.navigationController pushViewController:aboutUSVC animated:YES];
       
    } else if (indexPath.row == 3) {  // 系统设置
        
        SettingViewController *setvc = [[SettingViewController alloc] init];
        setvc.view.backgroundColor = zuiqianhuiColor;
        [self.navigationController pushViewController:setvc animated:YES];
        
    }

}


///* OC */
//- (BOOL)prefersStatusBarHidden {
//
//    return YES;
//}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    //设置导航条透明
//    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];

    [self.navigationController setNavigationBarHidden:YES animated:YES];
    //去掉透明后导航栏下边的黑边
//    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    // 状态栏字体背景颜色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    // 状态栏字体背景颜色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)dealloc{
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeUserInfo" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"upDataUserInfo" object:nil];
}

@end
